package zzjson.rabbitmq.limit;

import com.rabbitmq.client.*;
import zzjson.rabbitmq.BaseRabbitMq;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.rabbitmq.limit</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月26日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class LimitConsumer {
	public static String QUEUE_NAME = "LIMIT_QUEUE";


	public static void main(String[] args) throws IOException {
		Connection conn = BaseRabbitMq.getConn();
		Channel channel = conn.createChannel();

		System.out.println("consumer 1 waiting for message");

		Consumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
				String msg = new String(body, StandardCharsets.UTF_8);
				System.out.println("consumer1 received message:" + msg);
				channel.basicAck(envelope.getDeliveryTag(), true);
			}
		};
		//非自动确认消息的前提下，如果一定数目的消息（
		// 通过consumer或者channel设置qos的值，未被确认前，不进行消费新的消息
		// ）
		channel.basicQos(2);
		channel.basicConsume(QUEUE_NAME, false, consumer);
		// BaseRabbitMq.close(channel, conn);

	}
}